/*
 * Copyright 2020-2021 redragon.dongbin
 *
 * This file is part of redragon-erp/赤龙ERP.

 * redragon-erp/赤龙ERP is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.

 * redragon-erp/赤龙ERP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with redragon-erp/赤龙ERP.  If not, see <https://www.gnu.org/licenses/>.
 */
package com.erp.prod.resource.controller;

import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.commons.lang.StringUtils;
import org.hibernate.exception.ConstraintViolationException;
import org.redragon.framework.hibernate.data.RDDataBox;
import org.redragon.framework.hibernate.model.Pages;
import org.redragon.framework.springmvc.ControllerSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.erp.dataset.service.DatasetCommonService;
import com.erp.masterdata.eco.service.MasterDataEcoService;
import com.erp.prod.resource.dao.model.ProdResource;
import com.erp.prod.resource.dao.model.ProdResourceCO;
import com.erp.prod.resource.service.ProdResourceService;

@Controller
@RequestMapping("/web/prodResource")
public class ProdResourceWebController extends ControllerSupport{
    
    //日志处理
    private Logger logger = LoggerFactory.getLogger(ProdResourceWebController.class);
    
    //服务层注入
    @Autowired
    private ProdResourceService prodResourceService;
    @Autowired
    private DatasetCommonService datasetCommonService;
    @Autowired
    private MasterDataEcoService masterDataEcoService;
    
    @Override
    public String getExceptionRedirectUrl() {
        return this.getFormatRedirectUrl("getProdResourceList");
    }
    
    
    
    /**
     * 
     * @description 查询数据列表
     * @date 2020-12-02 17:59:36
     * @author 
     * @param pages
     * @param prodResourceCO
     * @param model
     * @return String
     *
     */
    @RequestMapping("getProdResourceList")
    public String getProdResourceList(Pages pages, ProdResourceCO prodResourceCO, Model model) {
        //分页查询数据
        if(pages.getPage()==0) {
            pages.setPage(1);
        }
        
        //分页查询数据
        List<ProdResource> prodResourceList = this.prodResourceService.getDataObjects(pages, prodResourceCO);
        
        //资源类型
        Map resourceTypeMap = this.datasetCommonService.getProdResourceType();
        //计费类型
        Map resourceChargeTypeMap = this.datasetCommonService.getProdResourceChargeType();
        //基准类型
        Map resourceCountTypeMap = this.datasetCommonService.getProdResourceCountType();
        //单位
        Map materialUnitMap = this.datasetCommonService.getMaterialUnit();
        
        //审批状态
        Map approveStatusMap = RDDataBox.getApproveStatusMap();
        
        //页面属性设置
        model.addAttribute("prodResourceList", prodResourceList);
        model.addAttribute("pages", pages);
        model.addAttribute("resourceTypeMap", resourceTypeMap);
        model.addAttribute("resourceChargeTypeMap", resourceChargeTypeMap);
        model.addAttribute("resourceCountTypeMap", resourceCountTypeMap);
        model.addAttribute("materialUnitMap", materialUnitMap);
        model.addAttribute("approveStatusMap", approveStatusMap);
        
        return "basic.jsp?content=prodResource/prodResourceList";
    }
    
    
    
    /**
     * 
     * @description 查询单条数据
     * @date 2020-12-02 17:59:36
     * @author 
     * @param prodResource
     * @param model
     * @return String
     *
     */
    @RequestMapping("getProdResource")
    public String getProdResource(ProdResource prodResource, Model model) {
        //查询要编辑的数据
        if(prodResource!=null&&prodResource.getResourceId()!=null) {
            prodResource = this.prodResourceService.getDataObject(prodResource.getResourceId());
        }
        
        //资源类型
        Map resourceTypeMap = this.datasetCommonService.getProdResourceType();
        //计费类型
        Map resourceChargeTypeMap = this.datasetCommonService.getProdResourceChargeType();
        //基准类型
        Map resourceCountTypeMap = this.datasetCommonService.getProdResourceCountType();
        //单位
        Map materialUnitMap = this.datasetCommonService.getMaterialUnit();
        //审批状态
        Map approveStatusMap = RDDataBox.getApproveStatusMap();
        
        //页面属性设置
        model.addAttribute("prodResource", prodResource);
        model.addAttribute("resourceTypeMap", resourceTypeMap);
        model.addAttribute("resourceChargeTypeMap", resourceChargeTypeMap);
        model.addAttribute("resourceCountTypeMap", resourceCountTypeMap);
        model.addAttribute("materialUnitMap", materialUnitMap);
        model.addAttribute("approveStatusMap", approveStatusMap);
        
        return "basic.jsp?content=prodResource/prodResourceEdit";
    }
    
    
    
    /**
     * 
     * @description 编辑数据
     * @date 2020-12-02 17:59:36
     * @author 
     * @param prodResource
     * @param bindingResult
     * @param model
     * @return String
     *
     */
    @RequestMapping("editProdResource")
    public String editProdResource(@Valid ProdResource prodResource, BindingResult bindingResult, Model model, RedirectAttributes attr) {
        //参数校验
        Map<String, String> errorMap = this.validateParameters(bindingResult, model);
        if(errorMap.size()>0) {
            return "forward:getProdResource";
        }
        
        try {
            //对当前编辑的对象初始化默认的字段
            
            //保存编辑的数据
            this.prodResourceService.insertOrUpdateDataObject(prodResource);
            //提示信息
            attr.addFlashAttribute("hint", "success");
        }catch(Exception e){
            if(e.getCause().getClass()==ConstraintViolationException.class) {
                //提示信息
                model.addAttribute("hint", "hint");
                model.addAttribute("alertMessage", "编码已存在，请重新输入");
                return "forward:getProdResource";
            }else {
                throw e;
            }
        }
        
        return "redirect:getProdResourceList";
    }
    
    
    
    /**
     * 
     * @description 删除数据
     * @date 2020-12-02 17:59:36
     * @author 
     * @param prodResource
     * @return String
     *
     */
    @RequestMapping("deleteProdResource")
    public String deleteProdResource(ProdResource prodResource, RedirectAttributes attr) {
        //删除数据前验证数据
        if(prodResource!=null&&prodResource.getResourceId()!=null) {
            //删除数据
            this.prodResourceService.deleteDataObject(prodResource);
            //提示信息
            attr.addFlashAttribute("hint", "success");
        }
        
        return "redirect:getProdResourceList";
    }
    
    
    
    /**
     * 
     * @description 更新审批状态
     * @date 2020年8月4日
     * @author dongbin
     * @param code
     * @param approveStatus
     * @param attr
     * @return
     * @return String
     *
     */
    @RequestMapping("updateApproveStatus")
    public String updateApproveStatus(Integer id, String approveStatus, RedirectAttributes attr) {
        
        if(id!=null&&StringUtils.isNotBlank(approveStatus)) {
            //更新审核状态
            this.prodResourceService.updateApproveStatus(id, approveStatus);
            //提示信息
            attr.addFlashAttribute("hint", "success");
            attr.addAttribute("resourceId", id);
        }else {
            //提示信息
            attr.addFlashAttribute("hint", "hint");
            attr.addFlashAttribute("alertMessage", "提交或审批数据错误");
            attr.addAttribute("resourceId", id);
        }
        
        return "redirect:getProdResource";
    }
}
